package de.tabuma.shopping.view;

import de.tabuma.lib.*;
import java.io.*;
import java.util.logging.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;
import de.tabuma.shopping.entity.*;
import de.tabuma.shopping.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.2 $
 * @since $Date: 2009/07/21 19:37:33 $
 */
public class overview extends tabumaview {

    private static Logger log =  Logger.getLogger(overview.class.getName());

    String orderNav;

    public overview(String servlet, Hashtable parameter, String orderNav) {
        super(servlet, parameter);
        this.orderNav = orderNav;
    }

    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out)  {
        try {

            String url = this.getServlet()+"?";
            url += this.getLinkAsAttribute();
            url += "&amp;" + orderNav + "=" + de.tabuma.shopping.constants.ORDERNAV_DETAILS;
            url += "&amp;" + de.tabuma.shopping.constants.ORDER_NR+"=";

            String orderStateOpen = constants.getValueFromProperties("orders.overview.state.open");
            String orderStateCanceled = constants.getValueFromProperties("orders.overview.state.canceled");
            String orderStatePayed = constants.getValueFromProperties("orders.overview.state.payed");
            String orderStateCanceled_Payed = constants.getValueFromProperties("orders.overview.state.canceled_payed");

            double allPrice = 0;

            NumberFormat nf = NumberFormat.getCurrencyInstance();
            nf.setCurrency(Currency.getInstance(Locale.GERMANY));

            DateFormat df = DateFormat.getDateTimeInstance();

            String textOrderNr = constants.getValueFromProperties("orders.overview.table.header.ordernr");
            String textOrderDate = constants.getValueFromProperties("orders.overview.table.header.orderdate");
            String textOrderPrice = constants.getValueFromProperties("orders.overview.table.header.orderprice");
            String textOrderState = constants.getValueFromProperties("orders.overview.table.header.orderstate");
            String textOrderTotal = constants.getValueFromProperties("orders.overview.table.bottom.total");
            String textOrderButton = constants.getValueFromProperties("orders.overview.button.start_new_order");

            List<Order> list = (List<Order>) request.getAttribute(constants.ORDER_LIST);
            List<LicensePackage> licensePackages = (List<LicensePackage>) request.getAttribute("LicensePackageList");

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            String subnav = orderNav;
            int subnavId = de.tabuma.shopping.constants.ORDERNAV_NULL;

            out.println("<div class=\"contentContent\">");

            licenseassignmenttable lat = new licenseassignmenttable(this.getServlet(), this.getParameter(), orderNav);
            lat.processRequest(context, request, response, out);

            out.println("<p class=\"contentContentHeader\">" + constants.getValueFromProperties("orders.overview.header") + "</p>");
            out.println("<table class=\"contenttable\">");
            out.println("<tr>");
            out.println("<th class=\"contentth\">" + textOrderNr + "</th>");
            out.println("<th class=\"contentth\">" + textOrderDate + "</th>");
            out.println("<th class=\"contentth\">" + textOrderPrice + "</th>");
            out.println("<th class=\"contentth\">" + textOrderState + "</th>");
            out.println("</tr>");

            List<OrderLicensePackageRelation> olprs = (List<OrderLicensePackageRelation>) request.getAttribute(constants.ORDER_LICENSEPACKAGERELATION_LIST);

            if(list!=null) {
                Iterator<Order> myIt = list.iterator();
                while(myIt.hasNext()) {
                    Order myOrder = myIt.next();
                    out.println("<tr>");
                    out.println("<td class=\"contenttdright\"><a href=\""+url+myOrder.getId()+"\">"+myOrder.getId()+"</a></td>");
                    double price = 0.0;

                    for(int i=0;i<olprs.size();i++) {
                        if(olprs.get(i).getOrderId()==myOrder.getId().intValue()) {
                            for(int j=0;j<licensePackages.size();j++) {
                                if(olprs.get(i).getLicensePackageId()==licensePackages.get(j).getId().intValue()) {
                                    price += licensePackages.get(j).getPrice();
                                }
                            }
                        }
                    }
                    
                    out.println("<td class=\"contenttdright\"><a href=\""+url+myOrder.getId()+"\">"+df.format(myOrder.getOrderDate())+"</a></td>");
                    out.println("<td class=\"contenttdright\"><a href=\""+url+myOrder.getId()+"\">"+nf.format(price)+"</a></td>");
                    switch(myOrder.getState()) {
                        case(constants.STATE_OPEN):
                            out.println("<td class=\"contenttdright\"><a href=\""+url+myOrder.getId()+"\">"+orderStateOpen+"</a></td>");
                            break;
                        case(constants.STATE_CANCELED):
                            out.println("<td class=\"contenttdright\"><a href=\""+url+myOrder.getId()+"\">"+orderStateCanceled+"</a></td>");
                            break;
                        case(constants.STATE_PAYED):
                            out.println("<td class=\"contenttdright\"><a href=\""+url+myOrder.getId()+"\">"+orderStatePayed+"</a></td>");
                            break;
                        case(constants.STATE_PAYED_CANCELED):
                            out.println("<td class=\"contenttdright\"><a href=\""+url+myOrder.getId()+"\">"+orderStateCanceled_Payed+"</a></td>");
                            break;
                    }
                    out.println("</tr>");
                    allPrice += price;
                }
            }

            out.println("<tr>");
            out.println("<td class=\"contenttdboldright\" colspan=\"2\">"+textOrderTotal+"</td>");
            out.println("<td class=\"contenttdboldright\">"+nf.format(allPrice)+"</td>");
            out.println("<td class=\"contenttdboldright\"></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<br/>");

            out.println(this.getFormBeginn(null));
            out.println(this.getHiddenAsAttribute());
            out.println("<input type=\"hidden\" name=\""+orderNav+"\" value=\""+de.tabuma.shopping.constants.ORDERNAV_LICENSEOVERVIEW+"\"/>");
            out.println("<input type=\"submit\" value=\""+textOrderButton+"\" class=\"loginbutton\"/>");
            out.println(this.getFormEnd());
            
            out.println("</div>");

        } 
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
    }

}
